<template>
  <div class="upload-container">
    <el-upload
      :multiple="false"
      :show-file-list="true"
      :on-success="handleImageSuccess"
      :on-error="error"
      :headers="header"
      :on-preview="onPreview"
      :on-remove="onRemove"
      :limit="1"
      class="image-uploader"
      drag
      :action="uploadUrl"
      :file-list="fileList"
      :before-upload="beforeUpload"
    >
      <i class="el-icon-upload" />
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div slot="tip" class="el-upload__tip">只能上传一个文件</div>
    </el-upload>
    <div class="template-download">
      <el-link @click="handleDownTemp">模版下载</el-link>
    </div>
    <!-- <div class="image-preview">
      <div v-show="imageUrl.length > 1" class="image-preview-wrapper">
        <img :src="imageUrl + '?imageView2/1/w/200/h/200'">
        <div class="image-preview-action">
          <i class="el-icon-delete" @click="rmImage" />
        </div>
      </div>
    </div> -->
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'

export default {
  name: 'SingleImageUpload',
  props: {
    value: {
      type: String,
      default: ''
    },
    // 下载api
    url: {
      type: String,
      default: ''
    },
    // 文件名
    fileName: {
      type: String,
      default: ''
    },
    // 下载参数
    params: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      uploadUrl: process.env.VUE_APP_FILE_UPLOAD_URL,
      baseUrl: process.env.VUE_APP_BASE_API,
      tempUrl: '',
      dataObj: { token: '', key: '' },
      header: {
        token: ''
      },
      fileList: []
    }
  },
  computed: {
    imageUrl () {
      return this.value
    }
  },
  created () {
    this.header.token = getToken()
  },
  methods: {
    addFile (file) {
      this.fileList.push(file)
    },
    error (e) {
      console.log(e)
    },
    rmImage () {
      console.log(123)
      this.emitInput('')
    },
    emitInput (val) {
      this.$emit('input', val)
    },
    handleImageSuccess (e) {
      console.log(e)
      this.emitInput(e)
    },
    onPreview (e) {
      console.log(e)
      this.$emit('preview', e.response ? e.response.data.fullurl : e.url)
    },
    onRemove (file, fileList) {
      console.log(file, fileList)
      const urlArr = []
      fileList.forEach((item) => {
        urlArr.push(item.response.data.url)
      })
      this.$emit('fileList', urlArr)
    },
    // 下载模版
    async handleDownTemp () {
      if (!this.url) {
        return this.$message.error('模版不存在')
      }
      const downloadElement = document.createElement('a')
      const href = this.baseUrl + this.url
      downloadElement.style.display = 'none'
      downloadElement.href = href
      downloadElement.download = this.fileName // 下载后文件名
      console.log(downloadElement)
      document.body.appendChild(downloadElement)
      downloadElement.click() // 点击下载
      document.body.removeChild(downloadElement) // 下载完成移除元素
    },
    beforeUpload (file) {
      const str = file.name.split('.')
      console.log(str)
      const arr = ['docx', 'doc', 'pdf', 'xlsx', 'xls']
      const type = arr.indexOf(str[str.length - 1]) !== -1
      if (!type) {
        this.$message.error('文件只能上传word，pdf，excel格式!')
      }
      return type
    }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/styles/mixin.scss';
.upload-container {
  width: 100%;
  position: relative;
  @include clearfix;
  .image-uploader {
    width: 60%;
    float: left;
  }
  .image-preview {
    width: 200px;
    height: 200px;
    position: relative;
    border: 1px dashed #d9d9d9;
    float: left;
    margin-left: 50px;
    .image-preview-wrapper {
      position: relative;
      width: 100%;
      height: 100%;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .image-preview-action {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      cursor: default;
      text-align: center;
      color: #fff;
      opacity: 0;
      font-size: 20px;
      background-color: rgba(0, 0, 0, 0.5);
      transition: opacity 0.3s;
      cursor: pointer;
      text-align: center;
      line-height: 200px;
      .el-icon-delete {
        font-size: 36px;
      }
    }
    &:hover {
      .image-preview-action {
        opacity: 1;
      }
    }
  }
  .template-download {
    float: right;
    margin-top: 80px;
    margin-right: 300px;
  }
}
</style>
